package com.lenovo.flutter_with_bmap;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.display.VirtualDisplay;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;

import androidx.annotation.NonNull;

import com.baidu.mapapi.map.TextureMapView;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.TextureRegistry;

public class TestPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {
    private FlutterPluginBinding flutterPluginBinding;
    private TextureRegistry.SurfaceTextureEntry surfaceTextureEntry;
    private Surface surface;
    private long textureId;
    private Random random = new Random();
    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        String method = call.method;
        switch(method){
            case "init":
                Log.e("地图开始init","我调用了init方法 textureId=" + textureId);
                Log.e("flutterPluginBinding","flutterPluginBinding=" + flutterPluginBinding);
                Map<String,Long> reply = new HashMap<>();
                reply.put("textureId",textureId);
                result.success(reply);
                break;
            case "render":
                Log.e("地图开始render------surface","我调用了render方法"+ surface);
                Log.e("flutterPluginBinding","flutterPluginBinding="+ flutterPluginBinding);
                Log.e("random","random="+ random);
                Canvas canvas = surface.lockCanvas(null);
                canvas.drawColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
                surface.unlockCanvasAndPost(canvas);
                result.success(null);
                break;
            default:break;
        }
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        Log.e("flutterPluginBinding","flutterPluginBinding这个时候开始有值了");
        flutterPluginBinding = binding;
        surfaceTextureEntry = flutterPluginBinding.getTextureRegistry().createSurfaceTexture();
        Log.e("surfaceTextureEntry=",surfaceTextureEntry + "=============");
        textureId = surfaceTextureEntry.id();
        surface = new Surface(surfaceTextureEntry.surfaceTexture());
        Log.e("surface=",surface + "");
        Log.e("注册method","我正在注册通道 test-view");
        new MethodChannel(flutterPluginBinding.getBinaryMessenger(),"test-view").setMethodCallHandler(this);
        try {
            Log.e("surface","surface" + surface);
            Canvas canvas = surface.lockCanvas(null);
//            surface.lockHardwareCanvas();
            Log.e("canvas","canvas" + canvas);
            canvas.drawColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            surface.unlockCanvasAndPost(canvas);
        }catch (Exception e){
            Log.e("错误",e.toString());
        }
;    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        Log.e("","我被销毁了");
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }
}
